import Vue from 'vue';
import iView from 'iview';
import Util from '../libs/util';
import VueRouter from 'vue-router';
import {
    routers,
    otherRouter,
    appRouter,
    commentRouters
} from './router';
Vue.use(VueRouter);

// 路由配置
const RouterConfig = {
    // mode: 'history',
    routes: routers
};

export const router = new VueRouter(RouterConfig);

router.beforeEach((to, from, next) => {
    iView.LoadingBar.start();
    Util.title(to.meta.title);
    if (localStorage.locking === '1' && to.name !== 'locking') { // 判断当前是否是锁定状态
        next({
            replace: true,
            name: 'locking'
        });
    } else if (localStorage.locking === '0' && to.name === 'locking') {
        next(false);
    } else {
        if (!localStorage.LoginTime && to.name !== 'login') { // 判断是否已经登录且前往的页面不是登录页
            next({
                name: 'login'
            });
        } else if (localStorage.LoginTime && to.name === 'login') { // 判断是否已经登录且前往的是登录页
            Util.title();
            next({
                name: 'home_index'
            });
        } else {
            const curRouterObj = Util.getRouterObjByName([otherRouter, ...appRouter], to.name);
            let permissoinCheck = false;
            if (parseInt(localStorage.access) !== 0) {
                let permissions = JSON.parse(localStorage.getItem('permissions')) || [];
                permissions = permissions.concat(commentRouters)
                if (!permissions) {
                    permissoinCheck = true;
                } else {
                    for (let i = 0; i < permissions.length; i++) {
                        if (to.name == permissions[i] || to.meta.parentName == permissions[i]) {
                            permissoinCheck = true;
                            break;
                        }
                    }
                }
            } else {
                permissoinCheck = true;
            }
            if (curRouterObj && curRouterObj.access !== undefined) { // 需要判断权限的路由
                if ((curRouterObj.access === parseInt(localStorage.access))) {
                    if (permissoinCheck) {
                        Util.toDefaultPage([otherRouter, ...appRouter], to.name, router, next); // 如果在地址栏输入的是一级菜单则默认打开其第一个二级菜单的页面
                    } else {
                        next({
                            replace: true,
                            name: 'error-403'
                        });
                    }
                } else {
                    next({
                        replace: true,
                        name: 'error-403'
                    });
                }
            } else { // 没有配置权限的路由, 直接通过
                if (permissoinCheck) {
                    Util.toDefaultPage([...routers], to.name, router, next);
                } else {
                    next({
                        replace: true,
                        name: 'error-403'
                    });
                }
            }
        }
    }
});

router.afterEach((to) => {
    Util.openNewPage(router.app, to.name, to.params, to.query);
    iView.LoadingBar.finish();
    window.scrollTo(0, 0);
});
